#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    sd=QSqlDatabase::addDatabase("QSQLITE");
    sd.setDatabaseName("D:\\Qt6\\QtProject\\server\\server.db");
    sd.open();
    sq=new QSqlQuery(sd);
    connect(ts,&QTcpSocket::connected,this,[=](){
        QJsonObject jo;
        qDebug()<<"success connect";
        jo.insert("datatype","databasecome");
        QJsonDocument jd1(jo);
        ts->write(jd1.toJson());
    });
    connect(ts,&QTcpSocket::disconnected,this,[=](){
        qDebug()<<"disconnect";
    });
    connect(ts,&QTcpSocket::readyRead,this,[=](){
        while(1)
        {
            QByteArray b=ts->read(1);
            std::string flag=b.toStdString();
            if(flag!="@")
            {
                ba.append(b);
            }
            else
                break;
        }
        qDebug()<<"ba-size:"<<ba.size();
        QJsonDocument jd=QJsonDocument::fromJson(ba);
        QJsonObject jo=jd.object();
        QString dt=jo.value("datatype").toString();
        qDebug()<<"datatype:"<<dt<<"is coming";
        if(dt=="askloaddatabase")
        {
            QJsonObject sendjo;
            int peoplenum=0;
            sq->exec("select * from peopleinfo");
            while(sq->next())
            {
                QString account=sq->value("account").toString();
                QString secret=sq->value("secret").toString();
                QString name=sq->value("name").toString();
                QString image=sq->value("headimage").toString();
                QString personalsign=sq->value("personalsign").toString();
                QString lastmes=sq->value("lastmes").toString();
                sendjo.insert(QString("account%1").arg(peoplenum),account);
                sendjo.insert(QString("secret%1").arg(peoplenum),secret);
                sendjo.insert(QString("name%1").arg(peoplenum),name);
                sendjo.insert(QString("image%1").arg(peoplenum),image);
                sendjo.insert(QString("personalsign%1").arg(peoplenum),personalsign);
                sendjo.insert(QString("lastmes%1").arg(peoplenum),lastmes);
                peoplenum++;
            }
            sendjo.insert("peoplenum",peoplenum);
            sendjo.insert("datatype","loaddatabase");
            QJsonDocument sendjd(sendjo);
            ts->write(sendjd.toJson());
        }
        else if(dt=="createtable")
        {
            QString account=jo.value("account").toString();
            qDebug()<<"account:"<<account<<"createtable";
            QString friendtable="account"+account+"friend";
            QString mestable="account"+account+"mes";
            if(!sq->exec(QString("create table '%1' (account TEXT)").arg(friendtable)))
                qDebug()<<"创建表"<<friendtable<<"failed";
            if(!sq->exec(QString("create table '%1' (senderaccount TEXT,receiveraccount TEXT,time TEXT,contend TEXT)").arg(mestable)))
                qDebug()<<"创建表"<<mestable<<"failed";
        }
        else if(dt=="insertpeopleinfo")
        {
            QString account=jo.value("account").toString();
            QString name=jo.value("name").toString();
            QString image=jo.value("image").toString();
            QString secret=jo.value("secret").toString();
            QString personalsign=jo.value("personalsign").toString();
            QString lastmes=jo.value("lastmes").toString();
            qDebug()<<name<<' '<<image<<" "<<secret;
            sq->prepare("insert into peopleinfo(account,secret,name,headimage,personalsign,lastmes) values(?,?,?,?,?,?)");
            sq->bindValue(0,account);
            sq->bindValue(1,secret);
            sq->bindValue(2,name);
            sq->bindValue(3,image);
            sq->bindValue(4,personalsign);
            sq->bindValue(5,lastmes);
            if(!sq->exec())
                qDebug()<<"insert database failed";
        }
        ts->waitForBytesWritten();
        ba.clear();
    });
    ts->connectToHost("127.9.0.5",8081);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    ts->write("fasdasd");
}

